﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class SelectSeat : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("SA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                hidOrderNo.Value = Request.QueryString["bk_id"];
                hidPaxSn.Value = Request.QueryString["pax_sn"];
                hidTripSn.Value = Request.QueryString["trip_sn"];

                lbtnQuery_Click(sender, e);
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            int ret_code;
            ret_code = ctlSelectSeat.RetreiveSeatMap(Convert.ToInt64(hidOrderNo.Value), Convert.ToInt16(hidPaxSn.Value), Convert.ToInt16(hidTripSn.Value), Session["AIRLINE"].ToString(), Session["OFFICE"].ToString(), true);
            switch (ret_code)
            {
                case 0:
                    //lblError.Text = "OK.";
                    break;
                case 1:
                    lblError.Text = "Ticket not found, or infant ticket, or all tickets in the order are checked-in!";
                    return;
                case 2:
                    lblError.Text = "Seat map not found!";
                    return;
                case 50:
                    lblError.Text = "Unauthorized airline!";
                    return;
                default:
                    lblError.Text = "Error:" + ret_code.ToString();
                    return;
            }
            return;
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("SA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }

            int ret_code;
            string key, row, col;
            string[] strArray2;
            if (Request.Form["SEAT"] == null)
            {
                lblError.Text = "Please refresh seat map and select a seat.";
                return;
            }
            key = Request.Form["SEAT"];
            strArray2 = key.Split('|');
            row = strArray2[0];
            col = strArray2[1];

            try
            {
                SqlCommand cmd = new SqlCommand("bll_tkt_select_seat");
                SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", hidOrderNo.Value);
                para = cmd.Parameters.AddWithValue("@pax_sn", hidPaxSn.Value);
                para = cmd.Parameters.AddWithValue("@trip_sn", hidTripSn.Value);
                para = cmd.Parameters.AddWithValue("@row", row);
                para = cmd.Parameters.AddWithValue("@col", col);
                para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Done.";
                        lbtnQuery_Click(sender, e);
                        break;
                    case 1:
                        lblError.Text = "Trip not found, or inproper status, or ticket expired!";
                        break;
                    case 3:
                        lblError.Text = "Flight status inproper!";
                        break;
                    case 10:
                        lblError.Text = "Select seat failed!";
                        break;
                    case 11:
                        lblError.Text = "Update ticket seating info failed!";
                        break;
                    case 12:
                        lblError.Text = "Release original selected seat failed!";
                        break;
                    case 50:
                        lblError.Text = "Unauthorized!";
                        break;
                    default:
                        lblError.Text = "Unexpected error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}